package com.suneast.suneaststore.modules.personal.order.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.common.entity.Picture;
import com.suneast.suneaststore.util.collection.StaticArrayList;

@SuppressLint("CheckResult")
public class CustomizePhotoAdapter extends RecyclerView.Adapter<CustomizePhotoAdapter.ViewHolder> {
    private StaticArrayList<Picture> list = new StaticArrayList<>(6);

    private OnItemChangedListener listener;
    private boolean editable = true;

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public Picture getItem(int i) {
        return list.get(i);
    }

    public interface OnItemChangedListener {
        void onCancel(String key);

        void onAdd(int position);
    }

    public void setOnItemChangedListener(OnItemChangedListener listener) {
        this.listener = listener;
    }

    public void refreshAll() {
        notifyDataSetChanged();
    }

    public void addItem(String path, String key) {
        if (list.add(new Picture(path, key))) {
            notifyItemChanged(list.size() - 1);
        }
    }

    public boolean isUploading() {
        for (int i = 0, len = list.size(); i < len; i++) {
            if (list.get(i).getState() == Picture.State.UPLOADING) {
                return true;
            }
        }
        return false;
    }

    public void notifyItemUploaded(boolean success, String key) { // success上传成功
        if (success) {
            for (int i = 0, len = list.size(); i < len; i++) {
                Picture pic = list.get(i);
                if (strEquals(key, pic.getKey()) && pic.getState() != Picture.State.FINISH) {
                    pic.setState(Picture.State.FINISH);
                    notifyItemChanged(i);
                }
            }
        } else {
            for (int i = 0; i < list.size(); ) {
                Picture pic = list.get(i);
                if (strEquals(key, pic.getKey()) && list.remove(i) != null) {
                    notifyItemRangeChanged(i, list.size() - i + 1);
                } else {
                    i++;
                }
            }
        }
    }

    public int available() {
        return list.size();
    }

    public int remaining() {
        return list.remaining();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_customize_photo, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Picture picture = list.get(position);
        if (picture == null) { //显示添加图片
            holder.ivPhoto.setImageResource(R.drawable.ic_add_photo);
            holder.ivPhoto.setOnClickListener(v -> {
                if (listener != null && editable) {
                    listener.onAdd(position);
                }
            });
            holder.ivOOXX.setOnClickListener(null);
            holder.ivPhoto.clearColorFilter();
        } else {
            if (editable) {
                holder.ivOOXX.setVisibility(View.VISIBLE);
                holder.ivOOXX.setOnClickListener(v -> {
                    String key = picture.getKey();
                    if (picture.isUploading() && list.remove(position) != null) {
                        if (listener != null) {
                            listener.onCancel(key);
                        }
                        notifyItemRangeChanged(position, list.size() - position + 1);
                    }
                });
            } else {
                holder.ivOOXX.setVisibility(View.GONE);
                holder.ivOOXX.setOnClickListener(null);
            }
            switch (picture.getState()) {
                case UPLOADING:
                    Glide.with(holder.ivPhoto.getContext())
                            .load(picture.getPath())
                            .into(holder.ivPhoto);
                    holder.ivPhoto.setDrawingCacheEnabled(true);
                    holder.ivPhoto.buildDrawingCache();
                    holder.ivPhoto.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                    holder.ivPhoto.setOnClickListener(null);
                    break;
                case FINISH:
                    Glide.with(holder.ivPhoto.getContext())
                            .load(picture.getPath())
                            .into(holder.ivPhoto);
                    holder.ivPhoto.clearColorFilter();
                    holder.ivPhoto.setOnClickListener(null);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.capacity();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivPhoto, ivOOXX;

        ViewHolder(View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.iv_customize_photo);
            ivOOXX = itemView.findViewById(R.id.iv_ooxx);
        }
    }

    // same as Objects.equals , used to keep compatible with low level apis
    private static boolean strEquals(String a, String b) {
        return a == b || (a != null && a.equals(b));
    }
}
